import { localCache } from '@/utils/cache'
import { BASE_URL, TIME_OUT } from './config'
import IRequest from './request'
import { LOGIN_TOKEN } from '@/constant'

const request = new IRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn(config) {
      const token = localCache.getCache(LOGIN_TOKEN)
      if (config.headers && token) {
        config.headers.Authorization = 'Bearer ' + token
      }
      return config
    },
    responseFailureFn(error) {
      return error
    }
  }
})

// export const instance2 = new IRequest({
//   baseURL: 'http://codercba.com:1888/airbnb/api',
//   timeout: 8000,

//   interceptors: {
//     requestSuccessFn(config) {
//       console.log('拦截器', config)
//       return config
//     },
//     requestFailureFn(error) {
//       console.log('请求拦截器错误', error)
//       return error
//     },
//     responseSuccessFn(response) {
//       console.log('响应拦截器', response)
//       return response
//     },
//     responseFailureFn(error) {
//       console.log('响应拦截器错误', error)
//       return error
//     }
//   }
// })

export default request
